package com.feicheng.controller.shop.wx.app.order;


import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Order;
import com.feicheng.service.IFOrderService;
import com.feicheng.service.IFPayService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/wx/app/order")
@RequestMapping("/wx/app/order")
public class OrderController {

    @Resource(name="pay-service")
    Map<String, IFPayService> payServiceMap;

    @Autowired
    IFOrderService fOrderService;

    @PostMapping("/info")
    public ResultInfo prePayOrder(
            Integer clientId,
            Integer storeId,
            String openId,
            Integer orderId
            ) {
        ResultInfo orderResult = fOrderService.getOrder(orderId);

        JSONObject orderMap = orderResult.getJSONObject(Constant.RESULT.DATA);
        Order order = orderMap.getObject(Constant.ENTITY.ORDER.TABLE, Order.class);

        if (Constant.VAL.ORDER_STATU.FIN.equals(order.getStatu())) {
            throw new SystemException("该订单已支付");
        }

        return payServiceMap.get(Constant.PAY_TYPE.WX).orderPay(orderId, clientId, storeId, order.getAmount(), openId);
    }

    @PostMapping("/notice-url/{storeId}")
    public JSONObject noticeOrderPay(
    		@RequestHeader("Wechatpay-Nonce") String nonce,
    		@RequestHeader("Wechatpay-Signature") String signature,
    		@RequestHeader("Wechatpay-Signature-Type") String signatureType,
    		@RequestHeader("Wechatpay-Timestamp") String timestamp,
    		@RequestHeader("Wechatpay-Serial") String serial,
    		@RequestBody String body, 
    		@PathVariable("storeId") Integer storeId) {
        payServiceMap.get(Constant.PAY_TYPE.WX)
        		.orderNotice(nonce, signature, signatureType, timestamp, serial, body, storeId);
        JSONObject result = new JSONObject();
        result.put("code", "SUCCESS");
        result.put("message", "成功");
        return result;
    }

    @GetMapping("/page")
    public ResultInfo getVipByPage(
            @RequestParam(defaultValue="1") Integer current,
            @RequestParam(defaultValue="10") Integer size,
            Integer storeId,
            Integer clientId) {

        JSONObject param = new JSONObject();
        param.put(Constant.HTTP.PARAM.CURRENT, current);
        param.put(Constant.HTTP.PARAM.SIZE, size);
        param.put(Constant.ENTITY.ORDER.STORE_ID, storeId);
        param.put(Constant.ENTITY.ORDER.CLIENT_ID, clientId);

        return fOrderService.getOrdersByPage(param);
    }
}
